
//
//  SampuxPCenterView.m
//  Sampux
//
//  Created by rzm on 16/7/5.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxPCenterView.h"
#import "IsValidateOrNot.h"
#import "SampuxCustomTextFiledView.h"

@interface SampuxPCenterView ()<UITextFieldDelegate>

@property (nonatomic, strong)  UIButton *titleBtn;
@property (nonatomic, strong)  UITextField *theNewPassword;
@property (nonatomic, strong) UITextField *confirmNewPassword;

@property (nonatomic, strong) SampuxCustomTextFiledView *passwordNumView;
@property (nonatomic, strong) SampuxCustomTextFiledView *theNewPasswordView;
@property (nonatomic, strong) SampuxCustomTextFiledView *confirmNewPasswordView;

@end
@implementation SampuxPCenterView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(modifyPassword:) name:NOTIFICATION_RESPONSE_UPDATEPWD object:nil];
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    CGFloat font = 8;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 17.;
    }
    
    CGRect phoneNumRect = CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2 , Sampux6HScale(60) + 64, Sampux6WScale(SampuxCustomW), Sampux6HScale(SampuxCustomH));
    _phoneNumView = [[SPDeleteTextFiledView alloc]initWithFrame:phoneNumRect];
    [self addSubview:_phoneNumView];
    [_phoneNumView createDeleteTextFiledWithPlaceholder:@"输入手机号码" andLeftImage:@"make-user" andRightImage:@"nil"];
    _phoneNumView.textFiled.delegate = self;
    _phoneNumView.textFiled.secureTextEntry = NO;
    _phoneNumView.textFiled.userInteractionEnabled = NO;

    CGRect passwordNumRect = CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2 , CGRectGetMaxY(_phoneNumView.frame) + SampuxMargin,  Sampux6WScale(SampuxCustomW), Sampux6HScale(SampuxCustomH));
    _passwordNumView = [[SampuxCustomTextFiledView alloc]initWithFrame:passwordNumRect];
    [self addSubview:_passwordNumView];
    [_passwordNumView createUserTextFiledWithPlaceholder:@"输入当前密码" andLeftImage:@"lock" andRightImage:@"eye"];
    _passwordNumView.textFiled.secureTextEntry = YES;
    _passwordNumView.textFiled.delegate = self;

    CGRect rect = CGRectMake(_passwordNumView.right - 80, CGRectGetMaxY(_passwordNumView.frame) + SampuxMargin, 80, Sampux6HScale(SampuxCustomH*0.6));
    _titleBtn = [UIButton createButtonWithTitle:@"忘记密码" frame:rect titleFont:THFont(font- 1) titleColor:THCodeBlueColor];
    [self addSubview:_titleBtn];
    _titleBtn.frame = rect;
    [_titleBtn addTarget:self action:@selector(titleBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    CGRect newpasswordRect = CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2, CGRectGetMaxY(_titleBtn.frame) + SampuxMargin, Sampux6WScale(SampuxCustomW),Sampux6HScale(SampuxCustomH));
    _theNewPasswordView = [[SampuxCustomTextFiledView alloc]initWithFrame:newpasswordRect];
    [self addSubview:_theNewPasswordView];
    [_theNewPasswordView createUserTextFiledWithPlaceholder:@"输入新密码" andLeftImage:@"lock" andRightImage:@"eye"];
    _theNewPasswordView.textFiled.secureTextEntry = YES;
    _theNewPasswordView.textFiled.delegate = self;

    CGRect makesureNewPasswordRect = CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2, CGRectGetMaxY(_theNewPasswordView.frame) + SampuxMargin, Sampux6WScale(SampuxCustomW),  Sampux6HScale(SampuxCustomH));
    _confirmNewPasswordView = [[SampuxCustomTextFiledView alloc]initWithFrame:makesureNewPasswordRect];
    [self addSubview:_confirmNewPasswordView];
    [_confirmNewPasswordView createUserTextFiledWithPlaceholder:@"再次输入新密码" andLeftImage:@"lock" andRightImage:@"eye"];
    _confirmNewPasswordView.textFiled.secureTextEntry = YES;
    _confirmNewPasswordView.textFiled.delegate = self;

    UIButton *btn = [UIButton createButtonWithTitle:@"确定" buttonType:UIButtonTypeCustom frame:CGRectMake(self.width/2 - Sampux6WScale(345/2)/2, _confirmNewPasswordView.bottom + Sampux6HScale(30), Sampux6WScale(345/2),Sampux6HScale(SampuxCustomH)) titleFont:[UIFont systemFontOfSize:font] titleColor:THDeFaultBlueColor backColor:THSystemClearColor];
    [self addSubview:btn];
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = THWhiteAlplaColor(0.6).CGColor;
    [btn setCorner:btn.height/2];
    [btn addTarget:self action:@selector(makeSureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *lab = [UILabel createLabel];
    lab.frame = CGRectMake(btn.x, btn.bottom + 20, btn.width, btn.height + 40);
    [self addSubview:lab];
    lab.backgroundColor = [UIColor orangeColor];

//    backView.height = btn.bottom + THMargin *2;
}

- (void)titleBtnClick
{
    if (self.forgetPWBlock) {
        self.forgetPWBlock();
    }
}

- (void)makeSureBtnClick
{
    
    BOOL isPassword = [IsValidateOrNot isValidatePassword:self.theNewPasswordView.textFiled.text];
    if ([self.passwordNumView.textFiled.text isEqualToString: @""]|| self.passwordNumView.textFiled.text.length == 0)
    {
        [SVProgressHUD showErrorWithStatus:@"密码错误"];

//        [MBProgressHUD showError:@"密码错误"];
    }else
    {
        if (!isPassword) {
            [SVProgressHUD showErrorWithStatus:@"密码必须为7~16位长度的字母，数字，下划线"];

//            [MBProgressHUD showError:@"密码必须为7~16位长度的字母，数字，下划线"];
        }else
        {
            if ([self.theNewPasswordView.textFiled.text isEqualToString:@""]||self.theNewPasswordView.textFiled.text.length == 0) {
                [SVProgressHUD showErrorWithStatus:@"密码必须为7~16位长度的字母，数字，下划线"];

//                [MBProgressHUD showError:@"密码必须为7~16位长度的字母，数字，下划线"];
            }else
            {
                if ([self.theNewPasswordView.textFiled.text isEqualToString: self.confirmNewPasswordView.textFiled.text]) {
                    [GBInterMaster_CommForSystem requestUserUpdatePwd:0 withOldPassword:self.passwordNumView.textFiled.text withNewPassword:self.theNewPasswordView.textFiled.text];
                }else
                {
                    [SVProgressHUD showErrorWithStatus:@"两次密码不一致"];

//                    [MBProgressHUD showError:@"两次密码不一致"];
                }
            }
        }
    }
}


- (void)modifyPassword:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"modifyPassword == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00){
        [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];

//        [MBProgressHUD showError:@"修改密码成功"];
        if (self.popBlock) {
            self.popBlock();
        }
    }else
    {
        [SVProgressHUD showErrorWithStatus:[SPResponseCodePrompt responseCodePrompt:responseCode]];

//        [MBProgressHUD showError:[SPResponseCodePrompt responseCodePrompt:responseCode]];
    }
}

#pragma -mark textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.passwordNumView.textFiled ||textField == self.theNewPasswordView.textFiled ||textField == self.confirmNewPasswordView.textFiled){
        NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (toBeString.length > 16 && range.length < 7){
            textField.text = [toBeString substringToIndex:16];
            return NO;
        }
        return YES;
    } else
    {
        return YES;
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, - 30 , kScreenWidth, kScreenHeight);
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
